package com.csaba.swing.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.util.Arrays;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.csaba.connector.model.HistoryItem;
import com.csaba.swing.MainWindow;
import com.csaba.swing.gui.util.DynamicColumnLayout;
import com.csaba.swing.gui.util.TableSizeContainer;
import com.csaba.swing.gui.util.WindowSizeContanier;

@SuppressWarnings( "serial" )
public class TransactionView extends JInternalFrame
{
	JLabel account;
	JLabel amount;
	JLabel date;
	JLabel description;
	JLabel balance;
	JTable nativeFields;

	HistoryItem item;

	public TransactionView()
	{
		super(MainWindow.getString("TransactionView.title"), true, true, true, true); //$NON-NLS-1$
		setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		if ( !WindowSizeContanier.getInstance().monitorComponent(this) )
			setSize(400, 350);
		setFrameIcon(GUIContext.getIcon("accounts", 16)); //$NON-NLS-1$

		//init layout
		final JPanel general = new JPanel(new DynamicColumnLayout(new int[] { DynamicColumnLayout.RIGHT,
				DynamicColumnLayout.LEFT }));
		general.add(new JLabel(MainWindow.getString("TransactionView.account"))); //$NON-NLS-1$
		general.add(account = new JLabel("")); //$NON-NLS-1$
		general.add(new JLabel(MainWindow.getString("TransactionView.amount"))); //$NON-NLS-1$
		general.add(amount = new JLabel("")); //$NON-NLS-1$
		general.add(new JLabel(MainWindow.getString("TransactionView.date"))); //$NON-NLS-1$
		general.add(date = new JLabel("")); //$NON-NLS-1$
		general.add(new JLabel(MainWindow.getString("TransactionView.description"))); //$NON-NLS-1$
		general.add(description = new JLabel("")); //$NON-NLS-1$
		general.add(new JLabel(MainWindow.getString("TransactionView.balance"))); //$NON-NLS-1$
		general.add(balance = new JLabel("")); //$NON-NLS-1$

		final JPanel buttons = new JPanel();
		buttons.add(new JButton(new AbstractAction(MainWindow.getString("TransactionView.button.close")) //$NON-NLS-1$
		{

			@Override
			public void actionPerformed( final ActionEvent arg0 )
			{
				dispose();
			}

		}));

		nativeFields = new JTable(new Object[0][2], new String[] { MainWindow.getString("TransactionView.header.name"), MainWindow.getString("TransactionView.header.value") }); //$NON-NLS-1$ //$NON-NLS-2$
		final JScrollPane scrollPane = new JScrollPane(nativeFields);
		nativeFields.setFillsViewportHeight(true);

		final JPanel contentPane = (JPanel) getContentPane();
		contentPane.setBorder(BorderFactory.createEmptyBorder());
		contentPane.setLayout(new BorderLayout());
		contentPane.add(general, BorderLayout.NORTH);
		contentPane.add(buttons, BorderLayout.SOUTH);
		contentPane.add(scrollPane, BorderLayout.CENTER);
	}

	public void showIt( final HistoryItem item )
	{
		this.item = item;

		account.setText(item.getOwner().toString());
		amount.setText(item.getAmount().toString());
		final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
		date.setText(dateFormat.format(item.getDate()));
		description.setText(GUIContext.formatEOL(item.getDescription()));
		balance.setText(item.getBalance().toString());

		final String[] propNames = item.getRemotePropertyNames();
		Arrays.sort(propNames);
		final String[][] tableModel = new String[propNames.length][2];
		for ( int i = 0; i < tableModel.length; i++ )
		{
			tableModel[i][0] = item.getLocalizedName(propNames[i]);
			tableModel[i][1] = item.getRemoteProperty(propNames[i]).toString();
		}
		nativeFields.setModel(new DefaultTableModel(tableModel, new String[] { MainWindow.getString("TransactionView.header.name"), MainWindow.getString("TransactionView.header.value") }) //$NON-NLS-1$ //$NON-NLS-2$
		{
			@Override
			public boolean isCellEditable( final int row, final int column )
			{
				return false;
			}
		});

		setVisible(true);
		TableSizeContainer.getInstance().monitorTable("TransactionView.nativeFields", nativeFields); //$NON-NLS-1$
	}

}
